var userRoutersSchema = require("../../model/myCenter/userRoutersModel");
var authControl = require("../../model/authControl/authControl");
const initRoute = () => {
  let defaultRoute = {
    "title": "首页模块",
    "ownerName": "system",
    "ownerId":"0",
    "children": [],
    "name": "home",
    "path": "/",
    "parentId": "0",
    "isMenu": true
  }
  let defaultRoute2 = {
    "title": "后台模块",
    "ownerName": "system",
    "ownerId":"0",
    "children": [],
    "name": "my",
    "path": "/my",
    "parentId": "0",
    "isMenu": true
  }
  let staticBackEndMenu = [
    {
      "title": "系统设置",
      "ownerName": "system",
      "name": "my",
      "path": "/my",
      "ownerId":"0",
      "children": [],
      "isMenu": true
    },
  ]
  let staticBackEndRouter = [
    {
      title: '权限控制',
      ownerName: "system",
      ownerId: '0',
      path: 'authControl',
      name: 'authControl',
      auth: 'authControl',
      cache: false,
      isWhite: true,
      hidden: false,
      isMenu: false,
    },
    {
      title: '菜单配置',
      path: 'menuSettings',
      ownerId: '0',
      ownerName: "system",
      name: 'menuSettings',
      auth: 'menuSettings',
      isMenu: false,
      cache: false,
      isWhite: true,
      hidden: false,
    },
  ]
  userRoutersSchema.find({}).then(result => {
    if (!result || result.length == 0) {
      new userRoutersSchema(defaultRoute).save()
      new userRoutersSchema(defaultRoute2).save().then(res => {
        userRoutersSchema.find({ title: '后台模块', name: 'my', isMenu: true, parentId: '0' }).then(data => {
          let newArr = staticBackEndMenu.map((item) => {
            item.parentId = data[0]._id
            item.parentName=data[0].name
            return item
          })
          userRoutersSchema.insertMany(newArr,{upsert:true,setDefaultsOnInsert:true,new:true}).then((datas,err) => {
            console.log(datas,err, 888)
            let newArr2 = staticBackEndRouter.map((item) => {
              item.parentId = datas[0]._id
              item.parentName = datas[0].name
              return item
            })
            userRoutersSchema.insertMany(newArr2,{upsert:true,setDefaultsOnInsert:true,new:true}).then(res2 => {
              console.log("路由初始化成功")
            })
          })
        })
      })
    }
  })
}
const initAuth = () => {
  let staticAuth = [{
    "label": "my",
    "value": "my",
    "userId": "0",
    "userName": "system"
  },
  {
    "label": "myCenter",
    "value": "myCenter",
    "userId": "0",
    "userName": "system"
  },
  {
    "label": "setUserInfo",
    "value": "setUserInfo",
    "userId": "0",
    "userName": "system"
  },
  {
    "label": "authControl",
    "value": "authControl",
    "userId": "0",
    "userName": "system"
  },
  {
    "label": "menuSettings",
    "value": "menuSettings",
    "userId": "0",
    "userName": "system"
  }]
  authControl.find({}).then(result => {
    if (!result || result.length == 0) {
      staticAuth.forEach(item => {
        new authControl(item).save()
      })
      console.log("权限初始化成功")
    }
  })
}
module.exports = {
  initRoute,
  initAuth
}